library(plotly)
library(dplyr)
library(readr)
mine_df =
read_csv("./data/VSRR_Provisional_Drug_Overdose_Death_Counts.csv") %>%
janitor::clean_names() %>%
filter(!(state_name %in% c("New York City", "United States"))) %>%
select(year, state_name, indicator, deaths = data_value) %>%
filter(indicator %in% "Number of Deaths") %>%
mutate(state= state.abb[match(state_name, state.name)],
hover = paste0(state_name, "\nDeath:", deaths))
## Rows: 42180 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): State, Month, Period, Indicator, Percent Complete, State Name, Foot...
## dbl (4): Year, Data Value, Percent Pending Investigation, Predicted Value
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
mine_df
## # A tibble: 3,876 × 6
## year state_name indicator deaths state hover
## <dbl> <chr> <chr> <dbl> <chr> <chr>
## 1 2015 Alaska Number of Deaths 4133 AK "Alaska\nDeath:4133"
## 2 2015 Alaska Number of Deaths 4222 AK "Alaska\nDeath:4222"
## 3 2015 Alaska Number of Deaths 4193 AK "Alaska\nDeath:4193"
## 4 2015 Alaska Number of Deaths 4084 AK "Alaska\nDeath:4084"
## 5 2015 Alaska Number of Deaths 4034 AK "Alaska\nDeath:4034"
## 6 2015 Alaska Number of Deaths 4220 AK "Alaska\nDeath:4220"
## 7 2015 Alaska Number of Deaths 4201 AK "Alaska\nDeath:4201"
## 8 2015 Alaska Number of Deaths 4101 AK "Alaska\nDeath:4101"
## 9 2015 Alaska Number of Deaths 4196 AK "Alaska\nDeath:4196"
## 10 2015 Alaska Number of Deaths 4219 AK "Alaska\nDeath:4219"
## # … with 3,866 more rows
font_style = list(
size = 15,
color = "black"
)
label = list(
bgcolor = "#EEEEEE",
bordercolor = "transparent",
font = font_style
)
death_graph = plot_geo(mine_df,
locationmode = "USA-states",
frame = ~year) %>%
add_trace(locations = ~state,
z = ~deaths,
zmin = 0,
zmax = max(pull(mine_df, deaths)),
color = ~deaths,
colorscale = "Electric",
text = ~hover,
hoverinfo = "text") %>%
layout(geo = list(scope = "usa"),
title = "Death by state in the US\n2015-2021") %>%
style(hoverlabel = label) %>%
config(displayModeBar = FALSE)
death_graph